package com.briup;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class TestSAX {
    public static void main(String[] args) {
        System.out.println("Hello World");
        TestSAX test = new TestSAX();
        test.saxParse("\"src\\\\main\\\\java\\\\com\\\\briup\\\\class.xml\"");
    }
    public void saxParse(String filePath) {
        //1.创捷工厂对象
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        try {
            //创捷解析器对象
            SAXParser saxParser = saxParserFactory.newSAXParser();
            //解析xml文件  重写DefaultHandler()类中方法，进行事件处理
            saxParser.parse(filePath,new DefaultHandler(){
                //最常用的5个事件
                @Override
                public void startDocument() throws SAXException {
                    System.out.println("解析器发现了文档的开始标签-文档开头");
                }

                public void endDocument() throws SAXException {
                    System.out.println("解析器发现了文档的结束标签-文档结束");
                }

                public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                    System.out.println("解析到应该开始标签");
                    System.out.println("qName:"+qName);
                }
            });

        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
